﻿package com.thorqq.magictimer;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.baidu.mobstat.StatService;
import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.timeLoop.QuickSetting;
import com.thorqq.magictimer.timeLoop.TimerMgr;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.FragmentActivityBase;
import com.thorqq.magictimer.widget.LineBreakLayout;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class FragmentActivityAddAction extends FragmentActivityBase
{
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private List<String>   mTitleList    = new ArrayList<String>();
    

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        initLayout();
        updateLayout();
        regListener();
    }
    
    private void regListener()
    {

    }
    
    public class TActionInfo
    {
        public String info;
        public Method method;
        public Object args[];
        
        TActionInfo(String i, Method m, Object a[])
        {
            info = i;
            method = m;
            if(a != null)
            {
                args = a.clone();
            }
        }
        
        TActionInfo(String i, Method m)
        {
            info = i;
            method = m;
            args = null;
        }

        TActionInfo()
        {
            
        }
    }

    private void initLayout()
    {        
        AudioManager audioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        int volRing = audioMgr.getStreamVolume(AudioManager.STREAM_RING);
        int volMusic = audioMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        int volNotification = audioMgr.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        
        List<TActionInfo> CommonList = new ArrayList<TActionInfo>();
        try
        {
            CommonList.add(new TActionInfo("闹铃", 
                    QuickSetting.class.getMethod("klaxon", (Class[])null)));
            CommonList.add(new TActionInfo("震动", 
                    QuickSetting.class.getMethod("vibrator", (Class[])null)));
            CommonList.add(new TActionInfo("通知", 
                    QuickSetting.class.getMethod("notification", (Class[])null)));
            CommonList.add(new TActionInfo("全屏提示", 
                    QuickSetting.class.getMethod("dialog", (Class[])null)));

            CommonList.add(new TActionInfo("进入飞行模式", 
                    QuickSetting.class.getMethod("EnterAirplaneMode", (Class[])null)));
            CommonList.add(new TActionInfo("退出飞行模式", 
                    QuickSetting.class.getMethod("LeaveAirplaneMode", (Class[])null)));

            CommonList.add(new TActionInfo("音量调节", 
                    QuickSetting.class.getMethod("volume", new Class[]{int.class, int.class, int.class}),
                    new Object[]{volRing, volMusic, volNotification}));
            CommonList.add(new TActionInfo("转为铃声", 
                    QuickSetting.class.getMethod("ringerNormal", (Class[])null)));
            CommonList.add(new TActionInfo("转为震动", 
                    QuickSetting.class.getMethod("ringerVibrate", (Class[])null)));
            CommonList.add(new TActionInfo("转为静音", 
                    QuickSetting.class.getMethod("ringerSilent", (Class[])null)));

            CommonList.add(new TActionInfo("清理内存", 
                    QuickSetting.class.getMethod("stopProcess", (Class[])null)));

        } 
        catch (Exception e)
        {
            Util.log_ex(e);
        } 
        mFragmentList.add(new FragmentAction(this, CommonList));
        mTitleList.add("常用");
        
        List<TActionInfo> CommunicationList = new ArrayList<TActionInfo>();
        try
        {
            CommunicationList.add(new TActionInfo("发送短信", 
                    QuickSetting.class.getMethod("sendSMS", (Class[])null)));
            CommunicationList.add(new TActionInfo("发送虚拟短信", 
                    QuickSetting.class.getMethod("sendVirtualSMS", (Class[])null)));
            CommunicationList.add(new TActionInfo("接收虚拟短信", 
                    QuickSetting.class.getMethod("recvVirtualSMS", (Class[])null)));
            CommunicationList.add(new TActionInfo("删除短彩信", 
                    QuickSetting.class.getMethod("deleteSMS", (Class[])null)));

            CommunicationList.add(new TActionInfo("拨打电话", 
                    QuickSetting.class.getMethod("phone", (Class[])null)));
//            CommunicationList.add(new TActionInfo("拨打虚拟电话", null));
//            CommunicationList.add(new TActionInfo("虚拟来电", null));
//
//            CommunicationList.add(new TActionInfo("打开数据链接", null));
//            CommunicationList.add(new TActionInfo("关闭数据链接", null));
        } 
        catch (Exception e)
        {
            Util.log_ex(e);
        } 
        mFragmentList.add(new FragmentAction(this, CommunicationList));
        mTitleList.add("通讯管理");

//        //其他
//        List<TAction> OtherctionList = new ArrayList<TAction>();
//        OtherctionList.add(new ActionInfo("天气预报", null));
//        OtherctionList.add(new ActionInfo("下载程序", null));
//        OtherctionList.add(new ActionInfo("启动程序", null));
//        mFragmentList.add(new FragmentAction(this, OtherctionList));
//        mTitleList.add("其他");
        
        super.setFragmentList(mFragmentList, mTitleList);
    }

    private void updateLayout()
    {
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        StatService.onPause(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        StatService.onResume(this);
    }

    public class NewActionListener implements OnClickListener 
    {
        Method mMethod;
        Object mArgs[];
        
        NewActionListener(Method method, Object args[])
        {
            mMethod = method;
            if(args != null)
            {
                mArgs = args.clone();
            }
        }
        
        @Override
        public void onClick(View arg0)
        {
            if(mMethod == null)
            {
                Toast.makeText(FragmentActivityAddAction.this, "未完成", Toast.LENGTH_SHORT).show();
                return;
            }
            
            try
            {
                mMethod.setAccessible(true);
                TAction action = (TAction) mMethod.invoke(null, mArgs);
                
                Intent i = new Intent();
                i.putExtra(TimerMgr.ALARM_INTENT_ACTION, action);
                setResult(RESULT_OK, i);
              
                Util.log("Add new action: " + action.getDescription() + ": " + action.toString());
                finish();
            } catch (Exception e)
            {
                Util.log_ex(e);
            } 

        }
        
    }

    public class FragmentAction extends Fragment 
    {

        Context mContext;
        List<TActionInfo> mActionList;
        
        private View mView;
        private ViewGroup mViewGroup;

        public FragmentAction(Context context, List<TActionInfo> actionList)
        {
            super();
            
            mContext = context;
            mActionList = actionList;
        }
       
        @Override
        public void onPause()
        {
            super.onPause();
        }

        public View makeView()
        {
            ScrollView scrollView = new ScrollView(mContext);
            scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            scrollView.setFillViewport(true);
            
            LineBreakLayout layout = new LineBreakLayout(mContext);  
            layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            layout.setPadding(0, 20, 0, 20);
                        
            for(int i = 0; i < mActionList.size(); i++)
            {
                Button btn = new Button(mContext);  
                btn.setTextAppearance(mContext, R.style.NewLoopPolicyBtnText);
                btn.setBackgroundResource(R.drawable.btn_new_policy_selector);
                btn.setText(mActionList.get(i).info);
                btn.setOnClickListener(new NewActionListener(mActionList.get(i).method, mActionList.get(i).args));
                layout.addView(btn);
            }
            scrollView.addView(layout);
                        
            return scrollView;
        }
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {                        
            if(mViewGroup != null)
            {
                mViewGroup.removeView(mView);
            }
            mViewGroup = new LinearLayout(mContext);
            
            if(mView == null)
            {
                mView = this.makeView();
            }
            
            mViewGroup.addView(mView);
            
            return mViewGroup;
        }
    }

    @Override
    protected void doOnClickSave()
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void doOnClickCancel()
    {
        // TODO Auto-generated method stub
        
    }
}
